<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/4/8
 * Time: 上午9:59
 */
class AnnouncementService extends Service{

    /**
     * 游戏公告列表表头
     * @return array
     */
    public static function indexHeader(){
        return array(
            'id'                => array('text' => '广播ID', 'rule' => 'int'),
            'startDate'         => array('text' => '开始时间'),
            'finish_info'       => array('text' => '发送状态'),
            'frequency_info'    => array('text' => '循环信息'),
            'frequency'         => array('text' => '循环间隔'),
            'type_info'         => array('text' => '类型'),
            'content'           => array('text' => '广播内容'),
            'action'            => array('text' => '操作')
        );
    }

    /**
     * 游戏公告列表数据
     */
    public static function indexData($params){
        $startTime  = strtotime($params['begin']);
        $endTime    = strtotime($params['end']) + 86400;

        $api = new ApiService($params['server']);
        $tmp = $api->announcementList($startTime, $endTime);

        if($tmp['state']) {
            $data = $tmp['data'];

            return $data;
        }else{
            return array();
        }
    }

    /**
     * 新增/修改公告信息
     * @param $params
     * @param null $id
     */
    public static function edit($server, $params, $id=null){
        $args = array(
            'id'                => (isset($id)) ? $id : -1,
            'title'             => $params['title'],
            'content'           => $params['content'],
            'date'              => $params['date'],
            'validity'          => $params['validity'],
            'isPopUpEveryLogin' => !empty($params['isPopUpFirstLogin']) ? 1 : 0,
            'isPopUpFirstLogin' => !empty($params['isPopUpFirstLogin']) ? 1 : 0,
            'version'           => $params['version']
        );

        $items = array();
        $i = 1;
        foreach($params['itemTitle'] as $k=>$v){
            if(isset($params['itemContent'][$k])){
                $items[] = array(
                    'content'   => $params['itemContent'][$k],
                    'title'     => $v,
                    'id'        => $i
                );
            }
            $i++;
        }
        $args['items'] = json_encode($items);

        $api = new ApiService($server);

        return $api->editAnnouncement($args);
    }

    public static function get($id){
        $tmp = ApiService::getAnnouncement($id);
        if($tmp['state']){
            $result = $tmp['data'];
            return $result;
        }else{
            return array();
        }
    }

    public static function delete($id){
        return ApiService::deleteAnnouncement($id);
    }
}